﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.ComponentModel;

public partial class frmUserList : BaseWinFrom
{
    public frmUserList()
    {
        InitializeComponent();
        new TextBoxBindMonthCalendar(txtCreateDate);
        //Bind(false);
    }

    Model.Search.UserBaseList GetQueryModel()
    {
        Model.Search.UserBaseList ubl = new Model.Search.UserBaseList();

        if (!string.IsNullOrEmpty(txtUserId.Text))
        {
            ubl.Id = txtUserId.Text;
        }
        if (!string.IsNullOrEmpty(txtUserName.Text))
        {
            ubl.Name = txtUserName.Text;
        }
        if (!string.IsNullOrEmpty(txtTelephone.Text))
        {
            ubl.Mobile = txtTelephone.Text;
        }
        if (!string.IsNullOrEmpty(txtEmail.Text))
        {
            ubl.Email = txtEmail.Text;
        }
        if (!string.IsNullOrEmpty(txtCreateDate.Text))
        {
            DateTime dt;
            if (DateTime.TryParse(txtCreateDate.Text, out dt))
            {
                ubl.MinCreate = FastJSON.PHP_DateTimeTransform.PHP_DT2LN(dt).ToString();
                ubl.MaxCreate = FastJSON.PHP_DateTimeTransform.PHP_DT2LN(dt.AddDays(1)).ToString();
            }
        }
        return ubl;
    }

    private void btSearch_Click(object sender, EventArgs e)
    {
        Bind();
    }

    private void Bind()
    {
        this.dgvUserList.AutoGenerateColumns = false;
        this.dgvUserList.DataSource = new BindingList<Model.UserBase>(new RST.UserBase().List(GetQueryModel()));

    }

    private void dgvUserList_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        if (e.RowIndex < 0)
            return;
        switch (e.ColumnIndex)
        {
            case 7:
                if (MessageBox.Show("确定删除该条记录", "删除", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    new RST.UserBase().Delete(new Model.UserBase() { Id = dgvUserList.Rows[e.RowIndex].Cells[0].Value.ToString() });
                }
                break;
            case 8:
                new frmUserBase(dgvUserList.Rows[e.RowIndex].Cells[0].Value.ToString()).Show();
                break;
            case 9:
                BindingList<Model.UserBase> li = dgvUserList.DataSource as BindingList<Model.UserBase>;
                Model.UserBase o = li[e.RowIndex];
                new frmDiagnosis(new Model.Diagnosis() { PatientId = o.Id, DiagnosisUserBase = new Model.DiagnosisUseBase { Name = o.Name, BirthDay = o.BirthDay, Email = o.Email, Mobile = o.Email, People = o.People, UserSex = o.UserSex } }).Show();
                break;
        }
    }

    private void txtCreateDate_Enter(object sender, EventArgs e)
    {

    }

    private void mcd_DateSelected(object sender, DateRangeEventArgs e)
    {
        txtCreateDate.Text = e.Start.ToString("yyyy-MM-dd 00:00:00");
        ((Control)sender).Visible = false;

    }
}